using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MimeCloud.AssetManagement.Business.Interface;
using MimeCloud.AssetManagement.Editor.Saving;
using MimeCloud.AssetManagement.ProviderServices;
using MimeCloud.AssetManagement.ProviderServices.Data;
using MimeCloud.AssetManagement.ProviderServices.Repository;
using MimeCloud.Foundations.ErrorLogging;

namespace MimeCloud.AssetManagement.RepositoryManagers.Web
{
    public abstract class WebStoreRepositoryManagerBase : IRepositoryManager
    {
        public byte[] GetSourceContentData(IAssetContent content)
        {
            //todo: download source content via http

            throw new System.NotImplementedException();
        }

        private int attemptsToRety = 4;
        public bool UploadAssetContentData(IAssetContent assetContent)
        {
            //assign unique name
            DisparateDataSaveManager.FileNameManager.AssignUniqueImageFileName(ref assetContent,
                                                                               delegate(IAssetContent content)
                                                                                   {
                                                                                       if (
                                                                                           AssetManagerConfig.
                                                                                               GetAssetSearcherProvider()
                                                                                               .ContentFromFullPath(
                                                                                               DataRepository,
                                                                                               content.AssetPath).Count >
                                                                                           0)
                                                                                           return
                                                                                               false;
                                                                                       else
                                                                                       {
                                                                                           return
                                                                                               true;
                                                                                       }
                                                                                   });
            int timesToReTry = Convert.ToInt32(attemptsToRety.ToString().Clone() as string);
            ;
            start:
            SaveProgressReporter(new AssetSaveProgressReport
                                     {
                                         Message =
                                             String.Concat("Uploading Image: ", assetContent.AssetPath, " (attempt ",
                                                           (Convert.ToInt32(attemptsToRety.ToString().Clone() as string) -
                                                            timesToReTry +
                                                            1).ToString(), " of ", attemptsToRety.ToString(), ")")
                                     });


            //hardupload file
            try
            {
                return HardUploadContentFile(assetContent);
            }
            catch (Exception ex)
            {
                //attempt retry for a certain number of times
                timesToReTry--;
                if (timesToReTry > 0)
                {
                    //log
                    ErrorHelper.LogError(new Exception("Error uploading file, retrying...", ex));
                    //Retry
                    goto start;
                }
                else
                    //attempted too many times, end and pass exception up
                    throw ex;
            }
        }



        protected abstract bool HardUploadContentFile(IAssetContent assetContent);

        public IAssetReaderFactory AssetStoreReader
        {
            get; set;
        }

        public string RepositoryKey
        {
            get; set;
        }

        public Dictionary<string, string> RepositorySettings
        {
            get; set;
        }

        public ReportSaveProgressDelegate SaveProgressReporter
        {
            get; set;
        }

        public IRepository DataRepository
        {
            get; set;
        }
    }
}
